package com.cychat.mall.service.impl;

import com.Cychat.commons.entity.pojo.Address;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cychat.mall.mapper.AddressMapper;
import com.cychat.mall.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Auther: 刘俊  <br/>
 * Date: 2025-11-5:上午 11:47  <br/>
 * Description: 地址服务实现类
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Override
    public List<Address> listByUserId(Long userId) {
        // 调用Mapper层方法查询用户地址列表
        return addressMapper.selectList(new LambdaQueryWrapper<Address>().eq(Address::getUserId, userId));
    }

    @Override
    public void setDefault(Integer addressId) {
        // 调用Mapper层方法设置默认地址
        addressMapper.update(null, new LambdaUpdateWrapper<Address>()
                .set(Address::getIsDefault, 1)
                .eq(Address::getAddressId, addressId));
    }

    @Override
    public Address getDefaultByUserId(Long userId) {
        // 调用Mapper层方法查询默认地址
        return addressMapper.selectOne(new LambdaQueryWrapper<Address>()
                .eq(Address::getUserId, userId)
                .eq(Address::getIsDefault, 1));
    }
}
